package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.*;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/hand-of-straights/">846.一手顺子</a>
 */
public class Solution846 extends BaseSolution {

    public boolean isNStraightHand(int[] hand, int groupSize) {
        Map<Integer, Integer> map = new TreeMap<>();
        for (int v : hand) {
            map.put(v, map.getOrDefault(v, 0) + 1);
        }
        for (int v : map.keySet()) {
            int c = map.get(v);
            if (c == 0) {
                continue;
            }
            for (int i = 1; i < groupSize; i++) {
                int nc = map.getOrDefault(v + i, 0);
                if (nc == 0) {
                    return false;
                }
                map.put(v + i, nc - c);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution846 solution = new Solution846();
        System.out.println(solution.isNStraightHand(new int[]
                        {1,2,3,6,2,3,4,7,8}
                , 3
                ));
    }
}